// pages/idleCommunityList/idleCommunityList.js
import api from "../../config/api"
import request from "../../utils/request"
import { stringify, getInf, getBarAndTitleHeight } from "../../utils/utils";
import {
	shareOptions
} from "../../utils/access";

Page({
	/**
	 * 页面的初始数据
	 */
	data: {
		communityList: [], //小区列表数据
		inputValue: "", //输入的内容
		cityName: "郑州市",
		// 信号栏高度
    ...getBarAndTitleHeight(),
	},

	// 获得小区列表
	async getCommunityList(value) {
		if (value === "") {
			// this.getUserCommunity()
			this.setData({
				communityList: [],
			});
		} else {
			const res = await await request.get(api.chooseCommunity,{
				'communityName': value
			})
			res.data.map((item,i)=>{
				let text = item.communityName;
				item.communityNameArr = getInf(text, value);
			})

			this.setData({
				communityList: res.data,
			});
		}
	},
	// 实时获取搜索值
	bindInputValue(e) {
		this.setData({
			inputValue: e.detail.value,
		});
		this.getCommunityList(this.data.inputValue);
	},
	// 取消按钮
	cancelFind() {
		let that = this;
		that.setData({
			inputValue: "",
		});
		that.getCommunityList(that.data.inputValue);
	},
	// 选择小区
	selectCommunity(e) {
		const item = {
			...e.currentTarget.dataset.item
		}
		let communityDetail = stringify({
			adcode: item.areaCode,
			address: item.address,
			adname: item.areaName,
			citycode: item.cityCode,
			cityname: item.cityName,
			id: item.communityCode,
			location: item.location,
			name: item.communityName,
			pcode: item.provinceCode,
			pname: item.provinceName,
		});
		wx.navigateTo({
			url: `/pages/idleCommunityAdd/idleCommunityAdd?communityDetail=${communityDetail}`,
		});
	},

	// 获取用户认证小区
	async getUserCommunity() {
		const res = await request.get(api.userCommunity)
		this.setData({
			communityList: res?.data ? [res?.data] : []
		})
		console.log('用户认证小区', res)
	},
	/**
	 * 生命周期函数--监听页面加载
	 */
	onLoad: function (options) {
		this.getUserCommunity()
	},

	/**
	 * 生命周期函数--监听页面初次渲染完成
	 */
	onReady: function () {},

	/**
	 * 生命周期函数--监听页面显示
	 */
	onShow: function () {},

	/**
	 * 生命周期函数--监听页面隐藏
	 */
	onHide: function () {},

	/**
	 * 生命周期函数--监听页面卸载
	 */
	onUnload: function () {},

	/**
	 * 页面相关事件处理函数--监听用户下拉动作
	 */
	onPullDownRefresh: function () {},

	/**
	 * 页面上拉触底事件的处理函数
	 */
	onReachBottom: function () {},

	// 用户点击右上角分享
	onShareAppMessage(e) {
		return shareOptions('default', e)
	},
});
